package com.example.springboot007servletsfilterslisteners.config;

import com.example.springboot007servletsfilterslisteners.servlet.FilterServlet;
import com.example.springboot007servletsfilterslisteners.servlet.LoginServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

import javax.security.auth.login.LoginContext;
import java.util.logging.Filter;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/2 下午 10:36
 * @version: v1.0
 */
@Configuration
public class WebAppConfig {
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        // 创建ServletRegistrationBean 登录一个或多个Servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new LoginServlet());
        registrationBean.addUrlMappings("/login");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new FilterServlet());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean addCommoneLogFilter(){
        FilterRegistrationBean  registrationBean = new FilterRegistrationBean();
        CommonsRequestLoggingFilter commonsRequestLoggingFilter = new CommonsRequestLoggingFilter();
        commonsRequestLoggingFilter.setIncludeQueryString(true);
        registrationBean.setFilter(commonsRequestLoggingFilter);
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }
}
